package com.oocl.o2o.client.service.impl;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.oocl.o2o.client.service.OrderItemService;
import com.oocl.o2o.client.vo.OrderItemVO;

/**
 * @description: 
 * @author  XIEMI2
 * @date Aug 14, 2017 10:45:38 AM
 * @version 1.0
 */
@Service
public class OrderItemServiceImpl implements OrderItemService{

	//totalMoney的计算在服务层
	@Override
	public Map<String, OrderItemVO> addOrderItem(Map<String, OrderItemVO> orderItems, OrderItemVO orderItemVO) {
		
		String orderItemVOId = orderItemVO.getId();
		OrderItemVO orderItemVO2 = orderItems.get(orderItemVOId);
		//购物车是否已包含该信息
		if(orderItems.containsKey(orderItemVOId)){ 
			int newTotal = orderItems.get(orderItemVOId).getTotal() + orderItemVO.getTotal();
			double newSubTotalMoney = orderItems.get(orderItemVOId).getSubTotalMoney() + orderItemVO.getSubTotalMoney();
			orderItemVO2.setTotal(newTotal);
			orderItemVO2.setSubTotalMoney(newSubTotalMoney);
			orderItems.put(orderItemVOId, orderItemVO2);
		}else{
			orderItems.put(orderItemVOId, orderItemVO);
		}
		return orderItems;
	}

	//核心是修改商品数量 和 总价
	@Override
	public Map<String, OrderItemVO> plusOneItem(Map<String, OrderItemVO> orderItems, OrderItemVO orderItemVO) {
		String orderItemVOId = orderItemVO.getId();
		OrderItemVO orderItemVO2 = orderItems.get(orderItemVOId);
		int newTotal = orderItemVO2.getTotal() + 1;
		double price = orderItemVO2.getPrice();
		double newSubTotalMoney = newTotal * price;
		orderItemVO2.setTotal(newTotal);
		orderItemVO2.setSubTotalMoney(newSubTotalMoney);
		orderItems.put(orderItemVOId, orderItemVO2);
		return orderItems;
	}

	@Override
	public Map<String, OrderItemVO> minusOneItem(Map<String, OrderItemVO> orderItems, OrderItemVO orderItemVO) {
		String orderItemVOId = orderItemVO.getId();
		OrderItemVO orderItemVO2 = orderItems.get(orderItemVOId);
		int newTotal = orderItemVO2.getTotal() - 1;
		double price = orderItemVO2.getPrice();
		double newSubTotalMoney = newTotal * price;
		orderItemVO2.setTotal(newTotal);
		orderItemVO2.setSubTotalMoney(newSubTotalMoney);
		orderItems.put(orderItemVOId, orderItemVO2);
		return orderItems;
	}

	@Override
	public Map<String, OrderItemVO> removeOneItem(Map<String, OrderItemVO> orderItems, String foodId) {
		orderItems.remove(foodId);
		return orderItems;
	}
	
	
}


